package com.example.composableexample.custom.fab

import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.graphics.drawable.DrawableWrapper
import androidx.compose.animation.core.Transition
import androidx.compose.animation.core.animateDp
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.animation.core.updateTransition
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.FloatingActionButton
import androidx.compose.material.Icon
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.ArrowRight
import androidx.compose.material.icons.filled.ArrowRightAlt
import androidx.compose.material.icons.filled.Check
import androidx.compose.material.icons.filled.Keyboard
import androidx.compose.material.ripple.rememberRipple
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.draw.scale
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.composableexample.ui.theme.ComposableExampleTheme

@OptIn(ExperimentalMaterial3Api::class)
@Preview
@Composable
fun MultiFloatingActionButtonPreview() {
    ComposableExampleTheme {
        var state by remember {
            mutableStateOf(MultiFabState.COLLAPSED)
        }
        Scaffold(
            floatingActionButton = {
                MultiFloatingActionButton(
                    fabIcon = Icons.Default.Add,
                    items = listOf(
                        MultiFabItem(
                            identifier = "new",
                            icon = Icons.Default.Check,
                            label = "New Item"
                        ),
                        MultiFabItem(
                            identifier = "existing",
                            icon = Icons.Default.ArrowRightAlt,
                            label = "Owned existing"
                        )
                    ),
                    multiFabState = state,
                    stateChanged = { state = it },
                    onFabItemChanged = {}
                )
            }
        ) {
            val alpha = if (state == MultiFabState.EXPANDED) 0.4f else 0f
            Box(
                modifier = Modifier
                    .alpha(animateFloatAsState(targetValue = alpha).value)
                    .fillMaxSize()
            )
        }
    }
}

@Composable
fun MultiFloatingActionButton(
    fabIcon: ImageVector,
    items: List<MultiFabItem>,
    multiFabState: MultiFabState,
    showLabel: Boolean = true,
    stateChanged: (MultiFabState) -> Unit,
    onFabItemChanged: (MultiFabItem) -> Unit
) {
    val transition: Transition<MultiFabState> = updateTransition(
        targetState = multiFabState,
        label = "fabState"
    )
    val scale: Float by transition.animateFloat(label = "scale") { state ->
        if (state == MultiFabState.EXPANDED) 56f else 0f
    }
    val rotation: Float by transition.animateFloat(label = "rotation") { state ->
        if (state == MultiFabState.EXPANDED) 45f else 0f
    }
    val alpha: Float by transition.animateFloat(
        label = "alpha",
        transitionSpec = { tween(durationMillis = 50) }
    ) { state ->
        if (state == MultiFabState.EXPANDED) 1f else 0f
    }
    val shadow: Dp by transition.animateDp(
        label = "shadow",
        transitionSpec = { tween(durationMillis = 50) }
    ) { state ->
        if (state == MultiFabState.EXPANDED) 2.dp else 0.dp
    }
    Column(horizontalAlignment = Alignment.End) {
        items.forEach { item ->
            MiniFabItem(
                item = item,
                alpha = alpha,
                shadow = shadow,
                scale = scale,
                showLabel = showLabel,
                onFabItemClicked = onFabItemChanged
            )
            Spacer(modifier = Modifier.height(12.dp))
        }
        FloatingActionButton(
            onClick = {
                stateChanged(
                    if (transition.currentState == MultiFabState.EXPANDED) {
                        MultiFabState.COLLAPSED
                    } else {
                        MultiFabState.EXPANDED
                    }
                )
            }
        ) {
            Icon(
                imageVector = fabIcon,
                contentDescription = "fabIcon",
                modifier = Modifier.rotate(rotation)
            )
        }
    }
}

@Composable
private fun MiniFabItem(
    item: MultiFabItem,
    alpha: Float,
    shadow: Dp,
    scale: Float,
    showLabel: Boolean,
    onFabItemClicked: (MultiFabItem) -> Unit
) {
    val fabColor = MaterialTheme.colorScheme.primary
    val shadowColor = MaterialTheme.colorScheme.onPrimary
    Row(
        verticalAlignment = Alignment.CenterVertically,
        modifier = Modifier.padding(end = 12.dp)
    ) {
        if (showLabel) {
            Text(
                text = item.label,
                fontSize = 12.sp,
                fontWeight = FontWeight.Bold,
                modifier = Modifier
                    .alpha(animateFloatAsState(targetValue = alpha).value)
                    .shadow(shadow)
                    .background(color = MaterialTheme.colorScheme.surface)
                    .padding(horizontal = 6.dp, vertical = 4.dp)
            )
            Spacer(modifier = Modifier.width(16.dp))
        }
//        Box(
//            contentAlignment = Alignment.Center,
//            modifier = Modifier
//                .size(32.dp)
//                .clip(shape = CircleShape)
//                .scale(scale)
//                .background(fabColor)
//                .clickable(
//                    onClick = { onFabItemClicked(item) },
////                    interactionSource = remember {
////                        MutableInteractionSource()
////                    },
////                    indication = rememberRipple(
////                        bounded = false,
////                        radius = 20.dp,
////                        color = MaterialTheme.colorScheme.onSecondary
////                    )
//                )
//        ) {
//            Icon(
//                imageVector = item.icon, contentDescription = item.label
//            )
//        }
        Canvas(
            modifier = Modifier
                .size(32.dp)
                .clickable(
                    onClick = { onFabItemClicked(item) },
                    interactionSource = remember {
                        MutableInteractionSource()
                    },
                    indication = rememberRipple(
                        bounded = false,
                        radius = 20.dp,
                        color = MaterialTheme.colorScheme.onSecondary
                    )
                )
        ) {
            drawCircle(
                color = shadowColor,
                center = Offset(x = center.x + 2f, y = center.y + 7f),
                radius = scale
            )
            drawCircle(color = fabColor, radius = scale)
            drawImage(
                image = ImageBitmap(width = item.icon.viewportWidth.toInt(), height = item.icon.viewportHeight.toInt()),
                topLeft = Offset(
                    x = (center.x) - (item.icon.viewportWidth / 2),
                    y = (center.y) - (item.icon.viewportWidth / 2)
                ),
                alpha = alpha
            )
        }
    }
}

@Composable
private fun Minimal(
    item: MultiFabItem,
    alpha: Float,
    scale: Float,
    onFabItemClicked: (MultiFabItem) -> Unit
) {
    val fabColor = MaterialTheme.colorScheme.secondary
    Canvas(
        modifier = Modifier
            .size(32.dp)
            .clickable(
                onClick = { onFabItemClicked(item) },
                interactionSource = remember {
                    MutableInteractionSource()
                },
                indication = rememberRipple(
                    bounded = false,
                    radius = 20.dp,
                    color = MaterialTheme.colorScheme.onSecondary
                )
            )
    ) {
        drawCircle(color = fabColor, radius = scale)
        drawImage(
            image = ImageBitmap(
                item.icon.defaultWidth.roundToPx(),
                item.icon.defaultHeight.roundToPx()
            ),
            topLeft = Offset(
                x = (center.x) - (item.icon.defaultWidth.roundToPx() / 2),
                y = (center.y) - (item.icon.defaultWidth.roundToPx() / 2)
            ),
            alpha = alpha
        )
    }
}